//
//  TTProbabilityMapEmpiric.m
//  iBeaconDemo
//
//  Created by cry on 17/1/10.
//  Copyright © 2017年 eGova. All rights reserved.
//

#import "TTProbabilityMapEmpiric.h"
#import "TTPointProbabilityMap.h"

@interface TTProbabilityMapEmpiric()
//距离信号点1米的abs(rssi)
@property (nonatomic, assign) double a;
//信号指数常量
@property (nonatomic, assign) double n;

@end

static double const EMPIRIC_PRPAGATION_CONSTANT = 10.0;
@implementation TTProbabilityMapEmpiric
////信号指数常量
//private double n;
////距离信号点1米的abs(rssi)
//private double a;

//private static final double EMPIRIC_PRPAGATION_CONSTANT = 10.0;

//public ProbabilityMapEmpiric(double n, int a) {
//    this.n = n;
//    this.a = a;
//}

- (instancetype)initWithN:(double)n andA:(double)a{
    
    if (self = [super init]) {
        self.a = a;
        self.n = n;
    }
    return self;
}

/**
 * 构造可能的坐标点
 *
 * @param xFrom
 * @param xTo
 * @param yFrom
 * @param yTo
 * @param granularity 划分粒度（精度）
 * @param x 选定的已知的参考的信号节点的X坐标
 * @param y 选定的已知的参考的信号节点的y坐标
 * @return the new empirical probability map
 */
//@Override
//public ArrayList<PointProbabilityMap> getProbabilityMap(double xFrom, double xTo,
//                                                        double yFrom, double yTo,
//                                                        double granularity,double x,double y) {
//    
//    ArrayList<PointProbabilityMap> pMap = new ArrayList<PointProbabilityMap>();
//    
//    for (double i = xFrom; i <= xTo; i += granularity) { // x-axis
//        for (double j = yFrom; j <= yTo; j += granularity) { // y-axis
//            double distance = 0;
//            if (i == 0 && j == 0) {
//                distance = granularity;
//            } else {
//                //					distance = Math.sqrt(i * i + j * j);
//                distance = Math.sqrt((x-i) * (x-i) + (y-j) * (y-j));
//            }
//            pMap.add(new PointProbabilityMap(i, j, distanceToRSSI(distance)));
//        }
//    }
//    return pMap;
//}

- (NSMutableArray<TTPointProbabilityMap *> *)getProbabilityMapWithXFrom:(double)xFrom xTo:(double)xTo yFrom:(double)yFrom yTo:(double)yTo granularity:(double)granularity x:(double)x y:(double)y{
    
    NSMutableArray<TTPointProbabilityMap *> *pMap = [NSMutableArray array];
    for (double i = xFrom; i <= xTo; i+= granularity) {
        for (double j = yFrom; j <= yTo; j+= granularity) {
            
            double distance = 0;
            if (i == 0 && j == 0) {
                distance = granularity;
            }else{
                distance = sqrt((x - i)* (x - i) + (y - j)* (y - j));
            }
            [pMap addObject:[[TTPointProbabilityMap alloc] initWithX:i
                                                                   Y:j
                                                           rssiValue:[self distanceToRSSIWithDistance:distance]]];
        }
    }
    return pMap;
}

/**
 * 根据距离换算rssi
 * empirical equation.
 *
 * @param distance the distance
 * @return the rssi value
 */
//public double distanceToRSSI(double distance) {
//    double rssi = -(ProbabilityMapEmpiric.EMPIRIC_PRPAGATION_CONSTANT * this.n * Math.log10(distance) + this.a);
//    return rssi;
//}
- (double)distanceToRSSIWithDistance:(double)distance{
    return - (EMPIRIC_PRPAGATION_CONSTANT * self.n * log10(distance) + self.a);
}

@end
